package org.dstart.object.menulist.sub
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.DisplayObject;
	import flash.text.TextFormat;

	public class MenuIteam extends Sprite
	{
		public static const CHANGE_FLAG:String	= "changeFlag";	
			/** 修改状态 **/
		
		public static var OUT_COLOR:uint	= 0x000000;
			/** 鼠标移上去时的颜色 **/
		
		public static var OVER_COLOR:uint	= 0xCC0000;
			/** 鼠标移开后的颜色 **/
		
		private var sprIcon:FlagIcons;		/** 图标 **/
		private var sprText:TextField;		/** 文字 **/
		
		public var info:*;					/** 信息对象 **/
		
		public function MenuIteam(showFlag:Boolean = false)
		{
			sprIcon = new FlagIcons(OUT_COLOR);
			sprText = new TextField();
			
			sprIcon.y = 3;

			sprText.x = sprIcon.x + sprIcon.width + 3;
			sprText.width = 100;
			sprText.height = 20;
			sprText.selectable = false;
			
			this.addChild(sprIcon);
			this.addChild(sprText);
			
			sprIcon.visible = showFlag;
			
			sprText.addEventListener(MouseEvent.ROLL_OVER, onOverText);
			sprText.addEventListener(MouseEvent.ROLL_OUT, onOutText);

			sprIcon.addEventListener(MouseEvent.CLICK, onChangeFlag);
			sprText.addEventListener(MouseEvent.CLICK, onChangeFlag);
		}
		
		/** 显示出来的文字内容 **/
		public function set text(str:String):void
		{
			sprText.width = 1000;
			sprText.textColor = OUT_COLOR;
			if(sprIcon.visible)
			{
				sprText.defaultTextFormat = new TextFormat(null, null, null, true);
			}
			else
			{
				this.sprText.defaultTextFormat = new TextFormat(null, null, null, false);
			}			
			sprText.text = str;	
			sprText.width = sprText.textWidth + 10;
		}		
		
		/** 设置标识状态 **/
		public function set model(flag:Boolean):void
		{
			sprIcon.model = flag;
		}
		
		/** 读取标识状态 **/
		public function get model():Boolean
		{
			return sprIcon.model;
		}
		
		/** 删除所有附加的子对象 **/
		public function DeleteSubObject():void
		{
			var delArray:Array = new Array();
			
			for(var i:int = 0; i < this.numChildren; i++)
			{
				var spr:DisplayObject = this.getChildAt(i);
				if(spr.name != sprIcon.name && spr.name != this.sprText.name)
				{
					delArray.push(spr);					
				}
			}
			
			while(delArray.length > 0)
			{
				this.removeChild(delArray.shift());
			}
		}
		
		/** 设置对象位置 **/
		public function SetPlace(xx:Number = 0, yy:Number = 0):void
		{
			this.x = xx;
			this.y = yy;
		}
		
		/***************************************/
				
		private function onOverText(e:MouseEvent):void
		{
			this.sprText.textColor = OVER_COLOR;
			this.sprText.defaultTextFormat = new TextFormat(null, null, null, true);
			this.sprText.text = this.sprText.text;
			this.sprIcon.color = OVER_COLOR;
		}
		
		private function onOutText(e:MouseEvent):void
		{
			this.sprText.textColor = OUT_COLOR;
			if(sprIcon.visible)
			{
				sprText.defaultTextFormat = new TextFormat(null, null, null, true);
			}
			else
			{
				this.sprText.defaultTextFormat = new TextFormat(null, null, null, false);
			}			
			this.sprText.text = this.sprText.text;
			this.sprIcon.color = OUT_COLOR;
		}
		
		private function onChangeFlag(e:MouseEvent):void
		{
			this.dispatchEvent(new Event(CHANGE_FLAG));
		}
	}
}